package com.huiyeji.hui.utils;

import com.huiyeji.hui.exception.HuiException;

/**
 * @author zhaoduo
 */
public class HuiResponse<T> {

    private Integer code;
    private String msg;
    private T data;

    private static <T> HuiResponse<T> ok(Integer code,String msg,T data){
        HuiResponse<T> response = new HuiResponse<>();
        response.code = code;
        response.msg = msg;
        response.data = data;
        return response;
    }

    public static <T> HuiResponse<T> ok(SuccessEnum successEnum,T data){
        return ok(successEnum.getCode(), successEnum.getMsg(), data);
    }

    public static <T> HuiResponse<T> ok(T data){
        return ok(SuccessEnum.DEFAULT,data);
    }

    public static <T> HuiResponse<T> ok(SuccessEnum successEnum){
        return ok(successEnum,null);
    }

    public static <T> HuiResponse<T> ok(){
        return ok(SuccessEnum.DEFAULT,null);
    }

    private static <T> HuiResponse<T> error(Integer code,String msg,T data){
        HuiResponse<T> response = new HuiResponse<>();
        response.code = code;
        response.msg = msg;
        response.data = data;
        return response;
    }

    public static <T> HuiResponse<T> error(Exception e){
        return error(ErrorEnum.DEFAULT.getCode(),e.getMessage(),null);
    }

    public static <T> HuiResponse<T> error(ErrorEnum errorEnum,T data){
        return error(errorEnum.getCode(), errorEnum.getMsg(), data);
    }

    public static <T> HuiResponse<T> error(String msg){
        return error(ErrorEnum.DEFAULT.getCode(),msg,null);
    }

    public static <T> HuiResponse<T> error(HuiException e){
        return error(e.getCode(),e.getMessage(),null);
    }

    public static <T> HuiResponse<T> error(ErrorEnum errorEnum){
        return error(errorEnum,null);
    }

    public static <T> HuiResponse<T> error(T data){
        return error(ErrorEnum.DEFAULT,data);
    }

    public static <T> HuiResponse<T> error(){
        return error(ErrorEnum.DEFAULT,null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
